package com.example.bbs.web.servlet; /**
 * @auther: zhangxin
 * @Description:
 */

import com.example.bbs.model.Post;
import com.example.bbs.model.User;
import com.example.bbs.service.PostService;
import com.example.bbs.service.impl.PostServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "PuPostServlet", value = "/bbs/pubPost")
public class PubPostServlet extends HttpServlet {

    PostService postService = new PostServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");
        String title = req.getParameter("title");
        String content = req.getParameter("content");
        int bid = Integer.parseInt(req.getParameter("bid"));
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("loginUser");
        Post post = new Post(title,user.getUserId(),bid,content);
        Boolean bool = postService.addPost(post);
        if(bool){
            resp.sendRedirect("bbs/posts?bid=" + bid);
        }else{
            resp.sendRedirect("/pub_post.jsp?lose=发帖失败&bid=" + bid);
        }
    }
}
